<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<?php header( 'Content-type: text/html; charset=utf-8' );
// Awesome Facebook Application
//
// Name: orktest
// 
//v1 displaying friends list 

require_once( "facebook-php-sdk/src/facebook.php" );
require_once ('utils/KLogger.php');
$log = new KLogger ( "log/fbapp_log.txt" , KLogger::DEBUG );

$facebook = new Facebook(array(
  'appId' => '127996350606219',
  'secret' => 'df23d57022f4f004adfc0a495309f806',
  'cookie' => true,
));
echo "------------------------[main]---------------------------<br>";
if($facebook)
{
	echo "[facebook Ok]<br>";
}
else
{
	echo "[facebook is null]<br>";
}
$uid = $facebook->getUser();
$sessioon = $facebook->getSession();
if($sessioon)
{
	echo "[sessioon Ok]<br>";
	echo "------------------------[user info]------------------------<br>";
	try {
		$me = $facebook->api('/me'); 
		$updated = date("l, F j, Y", strtotime($me['updated_time'])); 
		$gender = $me['gender'];

		echo "[id] " . $uid . "<br />";
		echo "[name] " . $me['name'] . "<br />";
		$log->LogInfo("User [" . $me['name'] . ", " . $uid . ", " . $_SERVER['REMOTE_ADDR'] . "]");
		if(array_key_exists('birthday',$me))
		{
			echo "[birthday] " . $me['birthday'] . "<br>";
		}				
		echo "[email] " . $me['email'] . "<br />";		
		echo "[gender] " . $gender . "<br>"; 		
		if(array_key_exists('hometown',$me))
		{
			echo "[hometown] " . $me['hometown'] . "<br>";
		}		
		if(array_key_exists('education',$me))
		{
			$education = $me['education'];				
			if(array_key_exists('school',$education[0]))		
			{				
				echo "[education] " . $education[0]['school']['name'] . "<br>";
			}
		}				
		echo "[link] " . $me['link'] . "<br />";				
		echo "[last updated time] " . $updated . "<br>"; 
		print_r(array_keys($me)); echo "<br>";			
		
		echo "------------------------[friends info]-----------------------<br>";
		$friendsGen = $facebook->api('/me/friends');	
		$friends = $friendsGen['data'];	
		print_r(array_keys($friends[0])); echo "<br>";			
		//echo "[friends] " . $friends['data'][0]['name'] . "<br />";				
		foreach($friends as &$fr)
		{	
			$birthday = "***";
			echo "<img src=\"https://graph.facebook.com/" 
			. $fr['id'] . "/picture\" title=\"" . $fr['name'] . "\"/>";
			echo " [name] " . $fr['name'] . ", [b]" . $birthday . "<br>";
		}				
	} 
	catch (FacebookApiException $e) { 
		echo "Error: FacebookApiException";
		echo "Error:" . print_r($e, true); 
	}		
}
else //--------registration------------------
{
	$log->LogInfo("Guest [" . $uid . ", " . $_SERVER['REMOTE_ADDR'] . "]");
?>	
    <script>
        FB.init({ 
            appId:'127996350606219', cookie:true, 
            status:true, xfbml:true 
         });
    </script>
    <fb:login-button perms="email,user_birthday,friends_birthday,user_status,user_checkins">
         Login with Facebook
    </fb:login-button>
<?php	
}
?>